﻿using System;
using System.Activities;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using Cinch;
using SLS.ExClassLib.Data;
using SLS.Controls;
using SLS.DataBase;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.Message;
using SLS.ExClassLib.ViewModel;
using SLS.ExClassLib.MVVM;
namespace SLS.Base
{
    public partial class SLBC : ViewData
    { 
        #region==================LOCAL PROPERTIES================

        int finishedcount = 0;
        int totalrecord = 1;
        #endregion
  
        #region==================PUBLIC PROPERTIES===============
        public SLDBSET SetGlobal
        {
            get { return DataAccessService.Instance.GlobalDataSet; }
        }
        public ViewModelBase ViewModelBase
        {
            get
            {
                return (ViewModelBase)this.DataContext;
            }
        }
        public IDataModel ViewDataSet
        {
            get
            {
                if (this.DataContext is ViewModelBase)
                {
                    return ((ViewModelBase)this.DataContext).ViewDataSet;
                }
                else
                {
                    return null;
                }
            }
        }
        public string MainWindowAddress {
            get
            {
                return "";
                //return DocumentManagement.Instance.ClientWFManagerAddress;
                //sun20121002
            }
        }
        ExDataRow row = null;
        public ExDataRow ActiveDataRow
        {
            get { return row; }
            set
            {
                row = value;
                if (row != null)
                {
                    if (ViewModelBase != null)
                    {
                        ViewModelBase.ActiveExDataRow = row;
                    }
                    //BuildEnvDataSet(row);
                }
            }
        }
        public DataRow RowBase
        {
            get;
            set;
        }
        public ExDataRow ExRowBase=new ExDataRow();   
        public NMICD NMICD
        {
            get
            {
                return (NMICD)this.FindName("NMICD");
            }
        }
        #region DataContext
        public SLQDBSET SetUQ
        {
            get
            {
                return ViewDataSet.SetUQ;
            }
        }
        public SLDBSET SetUI
        {
            get
            {
                return ViewDataSet.SetUI;
            }
        }
        public SLDBSET SetEnv
        {
            get
            {
                return ViewDataSet.SetEnv;
            }
        } 
        #endregion
        public string WorkflowXAML
        {
            get;
            set;
        }

        public Activity WFActivity;
        public bool flagJoinHLAFedrate = false;
        public bool flagJoinUIFedrate = false;
        public bool flagJoinWFFederate = false;
        public bool flagJoinWFSRVFederate = false;
        public string File4Dll = System.IO.Directory.GetCurrentDirectory() + @"\GUI\";
        public string File4WF = System.IO.Directory.GetCurrentDirectory() + @"\WorkFlow\";
        public bool flagEditEveryRecord
        {
            get;
            set;
        }
        public bool flagLoadGTPREDef
        {
            get;
            set;
        }
        public bool flagLoadGoodsMasterProperty
        {
            get;
            set;
        }
        public bool flagLoadGoodsMasterKeyRelation
        {
            get;
            set;
        }
        public bool flagLoadGoodsMasterAssemblePoint
        {
            get;
            set;
        }
        public bool flagActiveGoodsMasterProperty
        {
            get;
            set;
        }
        public bool flagActiveRecordAlreadySettled = false;
        public bool flagStartQueryUI = true;
        public bool flagLoadSet5KeyDef = true;
        public bool flagLoadSet4Reference = true;
        public bool flageLoadSet2Create = true;
        public SynchronizationContext _uiSyncContext = null;
        public bool HasXMLDataStore
        {
            get;
            set;
        }
        private string m_Address;
        public string Address
        {
            get { return m_Address; }
            set { m_Address = value; }
        } 
        public bool CheckGoodsMasterKeyRelation = true;
        public bool ValidateGoodsMasterKeyRelation = true;

        public Dictionary<string, object> SetUpdoc = new Dictionary<string, object>();
        public Dictionary<string, object> dicSource = new Dictionary<string, object>();
        public DataSet DataSetEvent;
        public DataTable TableBase
        {
            get
            {
                return SetUI.Tables[BaseTable];
            }
        }
        private DataTable m_TableBaseQueryCond;
        public DataTable TableBaseQueryCond
        {
            get { return m_TableBaseQueryCond; }
            set { m_TableBaseQueryCond = value; }
        }
        public ArrayList m_Table2CreateList;

        public string ExControlXMLPath
        {
            get
            {
                string p = System.IO.Path.Combine(DataAccessService.Instance.AppXMLPath, BaseTable);
                if (!System.IO.Directory.Exists(p))
                    System.IO.Directory.CreateDirectory(p);
                return System.IO.Path.Combine(DataAccessService.Instance.AppXMLPath, BaseTable, this.Name + ".xml");
            }
        }
        public string QueryCondXMLPath
        {
            get
            {
                string p = System.IO.Path.Combine(DataAccessService.Instance.AppXMLPath, BaseTable);
                if (!System.IO.Directory.Exists(p))
                    System.IO.Directory.CreateDirectory(p);
                return System.IO.Path.Combine(DataAccessService.Instance.AppXMLPath, BaseTable, this.Name + "QueryCond.xml");
            }
        }
        public string BaseTable
        {
            get;
            set;
        }
        public string TableQueryCond
        {
            get { return BaseTable + "QueryCond"; }      
        }

        public ExTabControl TabTable2Create
        {
            get
            {
                return (ExTabControl)FindName("DTabTable2Create");
            }
        }
        public string logRoute
        {
            get
            {
                return DateTime.Now + ":" + this.Name;
            }
        }
        /// <summary> 
        /// The ExChangeDataSet used to transfer the data passed from any external source to current content.
        /// </summary>
        public DataSet ExChangeDataSet
        {
            get;
            set;
        }
        /// <summary>
        /// Gets or set the root message of the content
        /// When resply some message to the workflow, the rootmessage will be needed.
        /// </summary>
        public MessageEx m_ExRootMessage;
        public MessageEx WFMessage
        {
            get;
            set;
        }
        public MessageEx BaseMessage
        {
            get
            {
                return DataAccessService.Instance.BaseMessage; ;
            }
        }
        public CollectionViewSource vwBaseTable
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwBaseTable");
            }
        }
        public CollectionViewSource vwBaseQueryCond
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwBaseQueryCond");
            }
        }
        Fluent.MenuItem sender;
        public string ListTable4Reference = "";
        public string ListTable2Create = "";
        public Queue ArrayTable2Create
        {
            set
            {
                if (value != null)
                {
                    ExtendTabTable2Create(value);
                }
            }
        }
        public Queue ArrayTable4Reference
        {
            set
            {
                if (value != null)
                {
                    ExtendStackTable4Reference(value);
                }
            }
        }
        public Queue ArrayTable5Property
        {
            set
            {
                if (value != null)
                {
                    ExtendShowTable5Property(value);
                }
            }
        }
        public bool flagLoadForeignKeyTable;
        #endregion

        #region==================CONSTRUCTION=================
        public virtual void ExtendTabTable2Create(Queue table2create)
        { }
        public virtual void ExtendStackTable4Reference(Queue table4ref)
        { }
        public virtual void ExtendShowTable5Property(Queue table5pro)
        { }
        public SLBC()
        {
            this.Loaded += new RoutedEventHandler(SLBC_Loaded);
        }
        public virtual void SLBC_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (vwBaseTable != null)
                {
                    if (vwBaseTable.View != null)
                    {
                        vwBaseTable.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        } 
        public virtual void HandleActiveDataRowChanged(DataRow dwTemp)
        {
            try
            {

            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion


    }
}
